/**
 * 
 */
package com.vanstone.imgapp.common.id.services.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vanstone.common.debug.MyAssert;
import com.vanstone.imgapp.common.ds.DataSourceHolder;
import com.vanstone.imgapp.common.id.IDSchema;
import com.vanstone.imgapp.common.id.SystemIDSchema;
import com.vanstone.imgapp.common.id.dal.IDTemplateDOMapper;
import com.vanstone.imgapp.common.id.dal.object.IDTemplateDO;
import com.vanstone.imgapp.common.id.services.IDService;

/**
 * @author shipeng
 *
 */
@Service("idService")
public class IDServiceImpl implements IDService {

  private static Logger LOG = LoggerFactory.getLogger(IDServiceImpl.class);

  @Autowired
  private IDTemplateDOMapper idTemplateDOMapper;

  @PostConstruct
  /*
   * (non-Javadoc)
   * 
   * @see example.business.api.id.services.IDService#initial()
   */
  @Override
  public void initial() {
    DataSourceHolder.switchWithyouID();
    SystemIDSchema[] tables = SystemIDSchema.values();
    if (tables == null || tables.length <= 0) {
      LOG.warn("not found table define in system id schema constant.");
      return;
    }
    List<String> tablenames = this.idTemplateDOMapper.select_tablenames();
    if (tablenames == null) {
      tablenames = new ArrayList<>();
    }
    for (SystemIDSchema schema : tables) {
      if (!tablenames.contains(schema.getSchemaName())) {
        idTemplateDOMapper.initial_table(schema.getSchemaName());
      }
    }
    DataSourceHolder.clear();
  }

  /*
   * (non-Javadoc)
   * 
   * @see example.business.api.id.services.IDService#nextID(example.business.api.id.IDSchema)
   */
  @Override
  public long nextID(IDSchema schema) {
    MyAssert.notNull(schema);
    DataSourceHolder.switchWithyouID();
    IDTemplateDO templateDO = new IDTemplateDO();
    templateDO.setTablename(schema.getSchemaName());
    this.idTemplateDOMapper.insert(templateDO);
    DataSourceHolder.clear();
    return templateDO.getId();
  }

  /*
   * (non-Javadoc)
   * 
   * @see example.business.api.id.services.IDService#clear(example.business.api.id.IDSchema)
   */
  @Override
  public void clear(IDSchema schema) {
    MyAssert.notNull(schema);
    DataSourceHolder.switchWithyouID();
    this.idTemplateDOMapper.delete_all(schema.getSchemaName());

  }

  @Override
  public void clear(IDSchema[] schemas) {
    MyAssert.notNull(schemas);
    for (IDSchema schema : schemas) {
      this.clear(schema);
    }
  }

  @Override
  public long max(IDSchema schema) {
    MyAssert.notNull(schema);
    DataSourceHolder.switchWithyouID();
    Long max = this.idTemplateDOMapper.select_max(schema.getSchemaName());
    if (max == null) {
      return 0;
    }
    DataSourceHolder.clear();
    return max;
  }
}
